'use strict';

const Controller = require('egg').Controller;
const Mock = require('mockjs');
const jwt = require('jsonwebtoken');

class HomeController extends Controller {


  async mock() {
    const { ctx, app } = this;

    const data = Mock.mock({
      'list|10-20': [
        {
          'classify|1': [ 'web', 'serve', 'language' ],
          'classifytow|1': [ 'javascript', 'java', 'ajax', 'node' ],
          title: '@ctitle(5,15)',
          img: '@image(100x100, @color)',
          info: '@cword(10,30)',
        },
      ],
    });

    // 将模拟好的数据 添加到数据库中
    data.list.forEach(async item => {
      await app.mysql.insert('list', { ...item });
    });

    ctx.body = {
      code: 1,
      msg: '添加成功',
    };
  }

  async getClassify() {
    const { ctx, app } = this;

    // 获取一级分类
    const data = await app.mysql.select('classify');

    ctx.body = {
      code: 1,
      data,
    };
  }

  async getClassifyTow() {
    const { ctx, app } = this;

    // 获取前端传来的参数
    const { type } = ctx.request.body;

    let data = null;

    if (type === 'all') {
      data = await app.mysql.query('select * from classifytwo');
    } else {
      data = await app.mysql.select('classifytwo', {
        where: {
          type,
        },
      });
    }

    // const data = await app.mysql.select('classifytwo', { type: 'web' });

    ctx.body = {
      code: 1,
      data,
    };

  }

  // 获取列表数据
  async getList() {
    const { ctx, app } = this;

    // 获取前端传来的数据
    let { value = '', towValue = '' } = ctx.request.body;

    if (value === 'all') {
      value = '';
    }

    const data = await app.mysql.query(`select * from list where classify like "%${value}%" and classifytow like "%${towValue}%"`);

    ctx.body = {
      code: 1,
      data,
    };
  }

  async getClassifyList() {
    const { ctx, app } = this;

    // 一级类型的表
    let classify = await app.mysql.select('classify');

    // 获取二级类型的表
    const classifytow = await app.mysql.select('classifytwo');

    classify = classify.map(item => {
      return {
        ...item,
        children: classifytow.filter(val => val.type === item.type),
      };
    });

    ctx.body = {
      code: 1,
      data: classify,
    };

  }

  async sendEdit() {
    const { ctx, app } = this;

    // 获取前端传来的参数
    const { id, title, text, html, classify, classifytow, info, img, username } = ctx.request.body;


    // 添加数据
    await app.mysql.insert('list', {
      title,
      text,
      html,
      classify,
      classifytow,
      info,
      userid: id,
      img,
      username,
    });

    // 获取用户信息
    const userInfo = await app.mysql.get('userlist', { id });

    // 修改作者的原创
    await app.mysql.update('userlist', { id, num: userInfo.num + 1 });

    ctx.body = {
      code: 1,
      msg: '添加成功',
    };

  }

  // 获取详情
  async getDetail() {
    const { ctx, app } = this;

    const { id } = ctx.request.body;

    const data = await app.mysql.get('list', { id });

    ctx.body = {
      code: 1,
      data,
    };
  }

  // 获取用户的文章
  async gettUserArticle() {
    const { ctx, app } = this;

    const { authorization } = ctx.request.headers;

    const id = jwt.verify(authorization, '1907A');

    const data = await app.mysql.select('list', {
      where: {
        userid: id,
      },
    });

    ctx.body = {
      code: 1,
      data,
    };
  }

  async attention() {
    const { ctx, app } = this;

    // id:用户id
    // userid 作者id
    const { id, userid } = ctx.request.body;

    // 通过用户id 获取到用户的关注列表
    const idInfo = await app.mysql.get('userlist', { id });

    // 将用户的关注列表转化为数组
    const likes = idInfo.likes.split(',');

    // 获取当前作者的详细信息
    const userIdInfo = await app.mysql.get('userlist', { id: userid });

    if (likes.some(item => item == userid)) {
      // 如果纯在说明已经关注过  取消关注
      await app.mysql.update('userlist', { id: userid, fans: userIdInfo.fans - 1 });

      // 当前用户的粉丝列表减去当前作者id
      await app.mysql.update('userlist', { id, likes: likes.filter(item => item != userid).join(',') });

    } else {
      await app.mysql.update('userlist', { id: userid, fans: userIdInfo.fans + 1 });

      await app.mysql.update('userlist', { id, likes: idInfo.likes + `${userid},` });
    }

    ctx.body = {
      code: 1,
      msg: '操作成功',
    };

  }
}

module.exports = HomeController;
